#!/usr/bin/python
# -*- coding: cp1250 -*-
from pepe.glib import *

def c2r(infl):
    '''Switch columns to rows

    warning:    works (for now) for reasonably big (meaning small) files only. 
                push it, and you will pay dearly
    syntax:     c2r.py <file name>
    '''
    lines = open(infl, 'r').readlines()
    of = open(infl+'.c2r#', 'w')
    res = ""; matrix = []; cd = ''
    # find out column delimiter
    if cd == '': cd = findcoldelimiter(lines[0])
    #try:
    for index, line in enumerate(lines):
        line = re.sub('\n', '', line)
        line = string.strip(line)

        if cd <> '': cols = string.split(line, cd)
        else: 
            cd = '\t'
            cols = [line, '']

        if line <> '':
            #print cols
            matrix.append(cols)
    res = zip(*matrix)
    """
    # zip function demo:
    table = [ ('Person', 'Disks', 'Books'),
              ('Zoe'   ,     12,      24 ),
              ('John'  ,     17,       5 ),
              ('Julien',      3,      11 )  ]         
    print zip(*table)
    >>>     [ ('Person', 'Zoe', 'John', 'Julien'),
              ('Disks' ,   12,     17,        3 ),
              ('Books' ,   24,      5,       11 ) ]
    """
    #print res
    for l in res:
        of.write(string.join(l, cd)+'\n')
    of.close()
    #except:
    #    print 'Some error!'

if __name__ == "__main__":
    if len(sys.argv) != 2:
        print c2r.__doc__
    else:
        c2r(sys.argv[1])